//src/store/counter.js
//使用持久化插件pinia-plugin-persist 持久化
import { defineStore } from "pinia";

export const useUserToken = defineStore('tokens', {
    // state: () => ({ token:"",name:'userToken' }),
    state: () => ({
        user:{
            token:"",
            id:0,
        },
        userInfo:{},
        //所有用户信息
        allUserInfo: [],
        //所有板块信息
        allCatsList: [],
        count: 0,
    }),
    getters: {// 使用箭头函数
        getToken: (state) => state.token,
        getUserInfo:(state) => state.userInfo,
        getAllUserList:(state) => state.allUserInfo,
        getAllCatsList:(state) => state.allCatsList,
        getAllUserLength:(state) => state.allUserInfo.length,
        getAllCatslength:(state) => state.allCatsList.length,
        doubleCount: (state) => state.count * 2,// 如果使用普通函数,不接收state也可以使用this
        doubleCount1() { return this.count * 2 },

    },
    actions: {
        setUserToken(token){
            console.log("设置token状态");
            this.user.token= token; // 注意属性名改为小写
        },
        setUserid(id){
            console.log("设置token状态");
            this.user.id= id; // 注意属性名改为小写
        },
        getUserToken(){
            console.log("获取token")
            return this.user.token; // 注意属性名改为小写
        },
        setAllCatsList(catsList){
            if(catsList != undefined){
                this.allCatsList = catsList
            }
        },
        setUserInfo(userInfo){
            this.userInfo=userInfo
        },
        setAllUserInfo(userList){
            if(userList != undefined){
                this.allUserInfo = userList
            }
        },
    },
    persist: {
        enabled: true,
    }

},)
